<?php
// {{{ abstract class PEAR2_LightDB_Abstract
/**
 * PEAR2 Abstract light
 */
abstract class PEAR2_LightDB_Abstract
{
    // {{{ Properties
    /**
     * The methods to use for the drivers
     */
    protected $_methods = array(
        'fetch'     => 'fetch_assoc',
        'connect'   => 'connect',
        'query'     => 'query',
        'select_db' => 'select_db',
        'errno'     => 'errno',
    );
    /**
     * The method prefix to use.
     */
    protected static $method_prefix;
    
    /**
     * The dsn information 
     */
    public $dsn;

    /**
     * Database connection
     */
    public $db;
    // }}}
    // {{{ public function getDSN ($param, $type = 'string')
    /**
     * Get DSN Value
     *
     * Get a peticular DSN Value.
     */
    public function getDSN($param, $type = 'string')
    {
        if (is_array($this->dsn)) {
            if (array_key_exists($param, $this->dsn)) {
                return $this->dsn[$param];
            }
        }
    }
    // }}}
    // {{{ public function setDSN($dsninfo)
    /**
     * Set DSN
     *
     * This method sets the DSN
     *
     * @param mixed $dsninfo   The datasource infos
     */
    public function setDSN($dsninfo)
    {
        $this->dsn = $dsninfo;
    }
    // }}}
    // {{{ public function connect()
    public function connect()
    {
        $username = $this->getDSN('username');
        $password = $this->getDSN('password');
        $hostname = $this->getDSN('hostname');
        $database = $this->getDSN('dbname');

        $method = self::$method_prefix . $this->_methods['connect'];
 
        $db = $method($hostname, $username, $password);
       
        if (!$db) {
            throw new PEAR2_LightDBException(PEAR2_LightDBException::FAILED_TO_CONNECT);
        }
        
        PEAR2_LightDBProperties::$isConnected = true;

        $this->db = $db;

        $select_db = self::$method_prefix . $this->_methods['select_db'];
        $select_db($database, $this->db);
        return $this->db;
    }
    // }}}
    // {{{ public function query($query, $type = null, $colnum = 0)
    /**
     * Query
     *
     * Executes a query on the database.
     */
    public function query($query, $type = null, $colnum = 0)
    {
        $queryMethod = self::$method_prefix . $this->_methods['query'];
        $e = $queryMethod($query);

        $errno = self::$method_prefix . $this->_methods['errno'];
        if ($errno($this->db)) {
            throw new PEAR2_LightDBException(PEAR2_LightDBException::QUERY_FAILED);
        }
        
        return $e;
    }
    // }}}
    // {{{ public function queryOne($query, $type = null, $colnum = 0)
    public function queryOne($query, $type = null, $colnum = 0)
    {
        throw new PEAR2_LightDBException('Method: ' . __METHOD__ . ' not implemented.');
    }
    // }}}
    // {{{ public function queryAll($query, $types = null, $fetchmod = false)
    /**
     * Query All
     *
     * This method will return a complete
     * resultset of a query into the memory.
     *
     * @return array  The resultset
     */
    public function queryAll($query, $types = null, $fetchmode = false)
    {
        $result = $this->query($query);

        $tmpResult = array();

        $method = self::$method_prefix . $this->_methods['fetch'];
        
        while ($tmp = $method($result)) {
            $tmpResult[] = $tmp;
        }

        return $tmpResult;
    }
    // }}}
    // {{{ public function queryRow($query, $type null, $colnum = 0)
    /**
     * Query a row only
     *
     * This method queries a row only.
     *
     * @todo implement method and document methods
     */
    public function queryRow($query, $type = null, $colnum = 0)
    {
        throw new PEAR2_LightDBException('Method: ' . __METHOD__ . ' not implemented.');
    }
    // }}}
    // {{{ public function queryCol($query, $type = null, $colnum = 0)
    public function queryCol($query, $type = null, $colnum = 0)
    {
        throw new PEAR2_LightDBException('Method: ' . __METHOD__ . ' not implemented.');
    }
    // }}}
}
// }}}
